<?php
	$html->css("/paid_listings/css/style.css",array(),null,false);
	function getTreeDisplayHtml($categoryId)
	{
		$children = cri("ListCategory")->childrenList($categoryId);
		if(count($children) == 0)
		{
			return "";
		}
		$out = "<ul>";
		foreach($children as $childCategoryId => $category)
		{
			$li = '<li id="category_'.$childCategoryId.'">';
			$li .= '<a href="#">'.$category.'</a>';
			$li .= getTreeDisplayHtml($childCategoryId);
			$li .= '</li>';
			$out .= $li;
		}
		$out .= "</ul>";
		return $out;
	}
	echo $form->create("Plan",array("url"=>"/".$this->params['url']['url'],'id'=>"plan_form"));
	$fields = cri("Plan")->getFields('group','add');
	foreach($fields as $field)
	{
		if($field == "duration_period")
		{
			continue;
		}
		if($field == "duration_number")
		{
			echo $form->input($field,array('div'=>array("class"=>"inline_fields"),"after"=>$form->input('duration_period',array("div"=>false))));
		}else
		{
			echo $form->input($field);
		}
	}
	echo $form->input('Plan.categories',array('type'=>"hidden"));
	echo $form->input('Plan.custom_fields.fieldsets',array('type'=>"hidden"));
	echo $form->input('Plan.custom_fields.fields',array('type'=>"hidden"));
	$javascript->link("/paid_listings/js/jquery.jstree.js",false);
	$javascript->link("/paid_listings/js/field_tree.js",false);
	$html->css('/paid_listings/css/jstree_style.css',null,array(),false);
?>
<h4><?php echo __("Assign categories and custom fields to this plan")?></h4>
<div style="width:100px;float:right;">
	<div id="loading_fields">
		
	</div>
</div>
<div class="unit size1of2 cat_tree">

	<strong><?php echo __("Categories")?></strong>
	<div id="cat_tree">
		<?php echo getTreeDisplayHtml(null);?>
	</div>
	
</div>
<div id="" class="unitExt size1of3 fielset_block">

	<div id="fielset_block_tree">
		
	</div>
</div>
<?php	echo $form->end(__("Save"));
?>
<script type="text/javascript">
	var CatTree;
	var prepareForm = function()
	{
		var neverExpire = $("#PlanDurationPeriod").children("option").filter("[value=\"\"]");
		if($('#PlanPaymentType').val() == "<?php echo Plan::PAYMENT_SUBSCRIPTION?>")
		{
			$(neverExpire).hide();
		}else
		{
			$(neverExpire).show();
		}
		if($('#PlanPaymentType').val() == "<?php echo Plan::PAYMENT_TRIAL?>")
		{
			$('#PlanPrice').attr('disabled',true);
                        $("#PlanDurationPeriod").val("").children("option").not(neverExpire).hide();
                        $('#PlanDurationNumber').hide().prev('label').hide();
 
		}else
		{
			$('#PlanPrice').attr('disabled',false);
                        $("#PlanDurationPeriod").children("option").not(neverExpire).show();
                        $('#PlanDurationNumber').show().prev('label').show();
		}
		if($('#PlanPlanType').val() == "<?php echo Plan::PLAN_TYPE_UPGRADE?>")
		{
			$('#PlanDisableModeration').parent().hide();
		}else
		{
			$('#PlanDisableModeration').show();
		}
		if($('#PlanLimitImages').attr('checked'))
		{
				$('#PlanImages').parent('div').show();
				$('#PlanAllowGallery').parent('div').show();
		}else
		{
			$('#PlanImages').parent('div').hide();
			$('#PlanAllowGallery').parent('div').hide();
		}

		if($('#PlanEnableOffers').val()==1)
		{
			$('#PlanActiveOffersLimit').parent('div').show();
		} else {
			$('#PlanActiveOffersLimit').val('0');
			$('#PlanActiveOffersLimit').parent('div').hide();
		}
		if($('#PlanEnablePromotions').val()==1)
		{
			$('#PlanActivePromotionsLimit').parent('div').show();
		} else {
			$('#PlanActivePromotionsLimit').val('0');
			$('#PlanActivePromotionsLimit').parent('div').hide();
		}
		if($('#PlanLimitEvents').val()==1)
		{
			$('#PlanAllowedEventsLimit').parent('div').show();
		} else {
			$('#PlanAllowedEventsLimit').val('0');
			$('#PlanAllowedEventsLimit').parent('div').hide();
		}
	}
	$(document).ready(function(){
		CatTree = 	$("#cat_tree").jstree({ "plugins" : [ "themes", "html_data", "checkbox", "sort", "ui" ],
		'checkbox' :{'two_state':false}});
		$('#PlanPaymentType').change(prepareForm);
		$("#PlanPlanType").change(prepareForm);
		$("#PlanLimitImages").change(prepareForm);
		$('#PlanEnableOffers').change(prepareForm);
		$('#PlanEnablePromotions').change(prepareForm);
		$('#PlanLimitEvents').change(prepareForm);
		prepareForm();
		$('#plan_form').submit(setValues);
		$.fn.prop = function(){
		}
		setTimeout(function(){
			initTree();
			$('#cat_tree').bind('change_state.jstree',function(){
				if(loadingFieldset == false)
				{	loadingFieldset = true;
					setTimeout(
					loadFieldsets,1000);
				}
			});
		},500);
	});
</script>